import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _find from "lodash/find";
import _isNumber from "lodash/isNumber";
import _forEach from "lodash/forEach";
import _isBoolean from "lodash/isBoolean";
import _isFunction from "lodash/isFunction";

/**
 * @date 2019-01-04
 */
import React from 'react';
import FieldProp from "./FieldProp";
import ComponentProp from "./ComponentProp";
import { attributeNameProp, attributeTypeProp, attributeValueProp, fieldLabelProp, fieldNameProp } from "../../config";
import DataType from "../../DataType";

var DynamicTabs = /*#__PURE__*/function (_React$Component) {
  _inherits(DynamicTabs, _React$Component);

  var _super = _createSuper(DynamicTabs);

  function DynamicTabs(props) {
    var _this;

    _classCallCheck(this, DynamicTabs);

    _this = _super.call(this, props);
    _this.handleFieldValuesChange = _this.handleFieldValuesChange.bind(_assertThisInitialized(_this));
    _this.handleComponentValuesChange = _this.handleComponentValuesChange.bind(_assertThisInitialized(_this)); // 拿到 form 的ref

    _this.editFormRef = React.createRef();
    return _this;
  }

  _createClass(DynamicTabs, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          field = _this$props.field,
          component = _this$props.component;

      if (field) {
        // edit field
        return /*#__PURE__*/React.createElement(FieldProp, Object.assign({}, this.props, {
          onValuesChange: this.handleFieldValuesChange,
          ref: this.editFormRef
        }));
      } else if (component) {
        // edit component
        return /*#__PURE__*/React.createElement(ComponentProp, Object.assign({}, this.props, {
          onValuesChange: this.handleComponentValuesChange,
          ref: this.editFormRef
        }));
      } else {
        // no edit component or field
        return null;
      }
    }
    /**
     * deal field's attribute change
     */

  }, {
    key: "handleFieldValuesChange",
    value: function handleFieldValuesChange(props, changeValues, allValues) {
      var _this2 = this;

      // 使用 allValues 重写 field 的属性
      var _this$props2 = this.props,
          field = _this$props2.field,
          component = _this$props2.component,
          onRefresh = _this$props2.onRefresh;

      var _ref = this.editFormRef.current || {},
          validateFields = _ref.validateFields;

      if (validateFields) {
        validateFields(function (err) {
          if (!err) {
            var newConfig = [];
            field[fieldLabelProp] = allValues[fieldLabelProp];
            field[fieldNameProp] = allValues[fieldNameProp]; // fields's common prop;
            // field.requiredFlag = allValues.requiredFlag;
            // // todo 字段的 visibleFlag 不使用了
            // field.visiableFlag = allValues.visiableFlag;
            // // todo 字段的 enabledFlag 不能编辑了
            // field.enabledFlag = allValues.enabledFlag;

            field.description = allValues.description;
            var getConfigOfPropValuesFunc = "get".concat(field.componentType, "ConfigOfPropValues");

            if (_this2[getConfigOfPropValuesFunc]) {
              _this2[getConfigOfPropValuesFunc](allValues, newConfig, field);
            }

            var prevFieldConfigs = field.config;
            field.config = newConfig.map(function (fieldConfig) {
              var prevFieldConfig = _find(prevFieldConfigs, function (prevC) {
                return prevC[attributeNameProp] === fieldConfig[attributeNameProp];
              });

              return _objectSpread(_objectSpread({}, prevFieldConfig), fieldConfig);
            });
            var newField = field; // { ...field };
            // 更新 feild 在 component 中的引用

            var fieldRefUpdate = false;

            _forEach(component.fields, function (f, index) {
              if (f === field) {
                fieldRefUpdate = true;
                component.fields[index] = newField;
                return false;
              }

              return !fieldRefUpdate;
            });

            if (_isFunction(onRefresh)) {
              onRefresh();
            }
          }
        });
      }
    }
    /**
     * deal component attribute change
     */

  }, {
    key: "handleComponentValuesChange",
    value: function handleComponentValuesChange(props, changeValues, allValues) {
      // 使用 allValues 重写 component 的属性
      // use allValues override component's props
      var _this$props3 = this.props,
          component = _this$props3.component,
          onRefresh = _this$props3.onRefresh;

      var _ref2 = this.editFormRef.current || {},
          validateFields = _ref2.validateFields;

      if (validateFields) {
        validateFields(function (err) {
          if (!err) {
            var newConfig = []; // common component's prop;

            component.templateCode = allValues.templateCode;
            component.description = allValues.description;
            component.enabledFlag = allValues.enabledFlag;

            if (_isNumber(allValues.style.marginBottom)) {
              var _newConfig$push;

              newConfig.push((_newConfig$push = {}, _defineProperty(_newConfig$push, attributeNameProp, 'style.marginBottom'), _defineProperty(_newConfig$push, attributeValueProp, allValues.style.marginBottom), _defineProperty(_newConfig$push, attributeTypeProp, DataType.Number), _newConfig$push));
            }

            var prevComponentConfigs = component.config;
            component.config = newConfig.map(function (componentConfig) {
              var prevComponentConfig = _find(prevComponentConfigs, function (prevC) {
                return prevC[attributeNameProp] === componentConfig[attributeNameProp];
              });

              return _objectSpread(_objectSpread({}, prevComponentConfig), componentConfig);
            });

            if (_isFunction(onRefresh)) {
              onRefresh();
            }
          }
        });
      }
    } // field props

  }, {
    key: "getTabPaneConfigOfPropValues",
    value: function getTabPaneConfigOfPropValues(allValues, newConfig) {
      if (_isBoolean(allValues.forceRender)) {
        newConfig.push({
          attributeName: 'forceRender',
          attributeType: DataType.Boolean,
          value: allValues.forceRender
        });
      }
    } // field props

  }]);

  return DynamicTabs;
}(React.Component);

export default DynamicTabs;